home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / RCSFNMS.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  27KB  |  921 lines

  1. /*
  2.  *                     RCS file name handling
  3.  */
  4. /****************************************************************************
  5.  *                     creation and deletion of /tmp temporaries
  6.  *                     pairing of RCS file names and working file names.
  7.  *                     Testprogram: define PAIRTEST
  8.  ****************************************************************************
  9.  */
  10.  
  11. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  12.    Copyright 1990 by Paul Eggert
  13.    Distributed under license by the Free Software Foundation, Inc.
  14.  
  15. This file is part of RCS.
  16.  
  17. RCS is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License as published by
  19. the Free Software Foundation; either version 1, or (at your option)
  20. any later version.
  21.  
  22. RCS is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with RCS; see the file COPYING.  If not, write to
  29. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  30.  
  31. Report problems and direct all questions to:
  32.  
  33.     rcs-bugs@cs.purdue.edu
  34.  
  35. */
  36.  
  37.  
  38.  
  39.  
  40. /* $Log: rcsfnms.c%v $
  41.  * Revision 1.2  1991/08/23  13:34:09  SGP
  42.  * Ported to MSDOS using Borland C++
  43.  *
  44.  * Revision 5.4  1990/11/01  05:03:43  eggert
  45.  * Permit arbitrary data in comment leaders.
  46.  *
  47.  * Revision 5.3  1990/09/14  22:56:16  hammer
  48.  * added more filename extensions and their comment leaders
  49.  *
  50.  * Revision 5.2  1990/09/04  08:02:23  eggert
  51.  * Fix typo when !RCSSEP.
  52.  *
  53.  * Revision 5.1  1990/08/29  07:13:59  eggert
  54.  * Work around buggy compilers with defective argument promotion.
  55.  *
  56.  * Revision 5.0  1990/08/22  08:12:50  eggert
  57.  * Ignore signals when manipulating the semaphore file.
  58.  * Modernize list of file name extensions.
  59.  * Permit paths of arbitrary length.  Beware file names beginning with "-".
  60.  * Remove compile-time limits; use malloc instead.
  61.  * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  62.  * Ansify and Posixate.
  63.  * Don't use access().  Fix test for non-regular files.  Tune.
  64.  *
  65.  * Revision 4.8  89/05/01  15:09:41  narten
  66.  * changed getwd to not stat empty directories.
  67.  * 
  68.  * Revision 4.7  88/08/09  19:12:53  eggert
  69.  * Fix troff macro comment leader bug; add Prolog; allow cc -R; remove lint.
  70.  * 
  71.  * Revision 4.6  87/12/18  11:40:23  narten
  72.  * additional file types added from 4.3 BSD version, and SPARC assembler
  73.  * comment character added. Also, more lint cleanups. (Guy Harris)
  74.  * 
  75.  * Revision 4.5  87/10/18  10:34:16  narten
  76.  * Updating version numbers. Changes relative to 1.1 actually relative
  77.  * to verion 4.3
  78.  * 
  79.  * Revision 1.3  87/03/27  14:22:21  jenkins
  80.  * Port to suns
  81.  * 
  82.  * Revision 1.2  85/06/26  07:34:28  svb
  83.  * Comment leader '% ' for '*.tex' files added.
  84.  * 
  85.  * Revision 4.3  83/12/15  12:26:48  wft
  86.  * Added check for KDELIM in file names to pairfilenames().
  87.  * 
  88.  * Revision 4.2  83/12/02  22:47:45  wft
  89.  * Added csh, red, and sl file name suffixes.
  90.  * 
  91.  * Revision 4.1  83/05/11  16:23:39  wft
  92.  * Added initialization of Dbranch to InitAdmin(). Canged pairfilenames():
  93.  * 1. added copying of path from workfile to RCS file, if RCS file is omitted;
  94.  * 2. added getting the file status of RCS and working files;
  95.  * 3. added ignoring of directories.
  96.  * 
  97.  * Revision 3.7  83/05/11  15:01:58  wft
  98.  * Added comtable[] which pairs file name suffixes with comment leaders;
  99.  * updated InitAdmin() accordingly.
  100.  * 
  101.  * Revision 3.6  83/04/05  14:47:36  wft
  102.  * fixed Suffix in InitAdmin().
  103.  * 
  104.  * Revision 3.5  83/01/17  18:01:04  wft
  105.  * Added getwd() and rename(); these can be removed by defining
  106.  * V4_2BSD, since they are not needed in 4.2 bsd.
  107.  * Changed sys/param.h to sys/types.h.
  108.  *
  109.  * Revision 3.4  82/12/08  21:55:20  wft
  110.  * removed unused variable.
  111.  *
  112.  * Revision 3.3  82/11/28  20:31:37  wft
  113.  * Changed mktempfile() to store the generated file names.
  114.  * Changed getfullRCSname() to store the file and pathname, and to
  115.  * delete leading "../" and "./".
  116.  *
  117.  * Revision 3.2  82/11/12  14:29:40  wft
  118.  * changed pairfilenames() to handle file.sfx,v; also deleted checkpathnosfx(),
  119.  * checksuffix(), checkfullpath(). Semaphore name generation updated.
  120.  * mktempfile() now checks for nil path; freefilename initialized properly.
  121.  * Added Suffix .h to InitAdmin. Added testprogram PAIRTEST.
  122.  * Moved rmsema, trysema, trydiraccess, getfullRCSname from rcsutil.c to here.
  123.  *
  124.  * Revision 3.1  82/10/18  14:51:28  wft
  125.  * InitAdmin() now initializes StrictLocks=STRICT_LOCKING (def. in rcsbase.h).
  126.  * renamed checkpath() to checkfullpath().
  127.  */
  128.  
  129.  
  130. #include "rcsbase.h"
  131.  
  132. libId(fnmsId, "$Id: rcsfnms.c%v 1.2 1991/08/23 13:34:09 SGP Exp $")
  133.  
  134. const char *RCSfilename;
  135. char *workfilename;
  136. struct stat RCSstat, workstat; /* file status for RCS file and working file */
  137. int haveworkstat;
  138.  
  139. static const char rcsdir[] = RCSDIR;
  140.  
  141.  
  142. #define TEMPNAMES 4 /* must be at least DIRTEMPNAMES (see rcsedit.c) */
  143. static char tfnames[TEMPNAMES][L_tmpnam];    /* unlink these when done */
  144. static volatile int tfmade[TEMPNAMES];        /* if these flags are set */
  145.  
  146.  
  147. struct compair {
  148.     const char *suffix, *comlead;
  149. };
  150.  
  151. static const struct compair comtable[] = {
  152. /* comtable pairs each filename suffix with a comment leader. The comment   */
  153. /* leader is placed before each line generated by the $Log keyword. This    */
  154. /* table is used to guess the proper comment leader from the working file's */
  155. /* suffix during initial ci (see InitAdmin()). Comment leaders are needed   */
  156. /* for languages without multiline comments; for others they are optional.  */
  157.     "a",   "-- ",   /* Ada         */
  158.         "c",   " * ",   /* C           */
  159.     "C",   "// ",    /* C++ in all its infinite guises */
  160.     "CC",  "// ",
  161.     "c++", "// ",
  162.     "cc",  "// ",
  163.     "cxx", "// ",
  164.     "cl",  ";;; ",  /* Common Lisp */
  165.     "cmf", "C ",    /* CM FORTRAN  */
  166.     "cs",  " * ",    /* C*          */
  167.     "el",  "; ",    /* Emacs Lisp  */
  168.     "f",   "c ",    /* Fortran     */
  169.     "for", "c ",
  170.         "h",   " * ",   /* C-header    */
  171.         "l",   " * ",   /* lex      NOTE: conflict between lex and franzlisp */
  172.     "lisp",";;; ",    /* Lucid Lisp  */
  173.         "mac", "; ",    /* macro       vms or dec-20 or pdp-11 macro */
  174.     "me",  ".\\\" ",/* me-macros   t/nroff*/
  175.     "ml",  "; ",    /* mocklisp    */
  176.     "mm",  ".\\\" ",/* mm-macros   t/nroff*/
  177.     "ms",  ".\\\" ",/* ms-macros   t/nroff*/
  178.     "p",   " * ",   /* Pascal      */
  179.     "pl",  "% ",    /* Prolog      */
  180.     "tex", "% ",    /* TeX           */
  181.         "y",   " * ",   /* yacc        */
  182.     nil,   "# "     /* default for unknown suffix; must always be last */
  183. };
  184.  
  185.  
  186.     void
  187. ffclose(FILE *fptr)
  188. /* Function: checks ferror(fptr) and aborts the program if there were
  189.  * errors; otherwise closes fptr.
  190.  */
  191. {       if (ferror(fptr) || fclose(fptr)==EOF)
  192.         IOerror();
  193. }
  194.  
  195.  
  196.  
  197.     char *
  198. maketemp(int n)
  199. /* Create a unique filename using n and the process id and store it
  200.  * into the nth slot in tfnames.
  201.  * Because of storage in tfnames, tempunlink() can unlink the file later.
  202.  * Returns a pointer to the filename created.
  203.  */
  204. {
  205.     char *p = tfnames[n];
  206.  
  207.     if (!tfmade[n]) {
  208. #if has_tmpnam
  209.         if (!tmpnam(p))
  210. #else
  211.         VOID sprintf(p, "%sRCS%cXXXXXX", tmp(), 'A'+n);
  212.         if (!mktemp(p))
  213. #endif
  214.             faterror("can't make temporary file name");
  215.     }
  216.     tfmade[n] = true;
  217.     return p;
  218. }
  219.  
  220.     void
  221. tempunlink()
  222. /* Clean up maketemp() files.  May be invoked by signal handler.
  223.  */
  224. {
  225.     register int i;
  226.  
  227.     for (i = TEMPNAMES;  0 <= --i;  )
  228.         if (tfmade[i]) {
  229.         VOID unlink(tfnames[i]);
  230.         tfmade[i] = 0;
  231.         }
  232. }
  233.  
  234.  
  235.     const char *
  236. bindex(register const char *sp,int ch)
  237. /* Function: Finds the last occurrence of character c in string sp
  238.  * and returns a pointer to the character just beyond it. If the
  239.  * character doesn't occur in the string, sp is returned.
  240.  */
  241. {
  242.     register const char c=ch, *r;
  243.         r = sp;
  244.         while (*sp) {
  245.                 if (*sp++ == c) r=sp;
  246.         }
  247.         return r;
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.     static void
  255. InitAdmin()
  256. /* function: initializes an admin node */
  257. {
  258.     register const char *Suffix;
  259.         register int i;
  260.  
  261.     Head=nil; Dbranch=nil; AccessList=nil; Symbols=nil; Locks=nil;
  262.         StrictLocks=STRICT_LOCKING;
  263.  
  264.         /* guess the comment leader from the suffix*/
  265.         Suffix=bindex(workfilename, '.');
  266.         if (Suffix==workfilename) Suffix= ""; /* empty suffix; will get default*/
  267.     for (i=0; comtable[i].suffix && strcmp(Suffix,comtable[i].suffix); i++)
  268.         ;
  269.     Comment.string = comtable[i].comlead;
  270.     Comment.size = strlen(comtable[i].comlead);
  271.         Lexinit(); /* Note: if finptr==NULL, reads nothing; only initializes*/
  272. }
  273.  
  274.  
  275. #if !RCSSEP
  276.     static int
  277. isRCSfilename(const char *f, const char *p)
  278. /* Yield true iff F (with pure file name P) is an RCS file name.  */
  279. {
  280.     return
  281.         p-f <= sizeof(rcsdir)-1  &&
  282.         ((p -= sizeof(rcsdir)-1) == f  ||  p[-1] == SLASH)  &&
  283.         strncmp(p, rcsdir, sizeof(rcsdir)-1) == 0;
  284. }
  285. #endif
  286.  
  287. #if RCSSEP
  288. #    define findpair(c,v,f,m) findpairfile(c,v,f)
  289. #else
  290. #    define findpair(c,v,f,m) findpairfile(c,v,f,m)
  291. #endif
  292.  
  293.     static char *
  294. #if RCSSEP
  295. findpairfile(int argc, char *argv[], char *fname)
  296. #else
  297. findpairfile(int argc, char *argv[], char *fname, int rcsmatch)
  298. /* *ARGV must be an RCS file name iff this is set.  */
  299. #endif
  300. /* Peek ahead in an ARGC-ARGV argument vector for a pathname ending in FNAME.
  301.  * Yield it if found, and set the corresponding pointer in ARGV to nil.
  302.  * Yield FNAME otherwise.
  303.  */
  304. {
  305.     register char *arg;
  306. #if !RCSSEP
  307.     register char *b;
  308. #endif
  309.     if (
  310.         0 < argc
  311. #if RCSSEP
  312.         && strcmp(bindex(arg = *argv,SLASH), fname) == 0
  313. #else
  314.         && strcmp(b = (char *)bindex(arg = *argv,SLASH), fname) == 0
  315.         && isRCSfilename(arg, b) == rcsmatch
  316. #endif
  317.     ) {
  318.         *argv = nil;
  319.         return arg;
  320.         }
  321.         return fname;
  322. }
  323.  
  324.  
  325.     static int
  326. handleworkstat(int s)
  327. {
  328.     if (s==0  &&  !S_ISREG(workstat.st_mode)) {
  329.         error("%s isn't a regular file", workfilename);
  330.         return false;
  331.     }
  332.     haveworkstat = errno;
  333.     return true;
  334. }
  335.  
  336. int getworkstat()
  337. /* Function: get status of workfilename. */
  338. {
  339.     errno = 0;
  340.     return handleworkstat(stat(workfilename, &workstat));
  341. }
  342.  
  343.     int
  344. getfworkstat(int f)
  345. /* Get status of file descriptor f. */
  346. {
  347.     errno = 0;
  348.     return handleworkstat(fstat(f, &workstat));
  349. }
  350.  
  351.  
  352. #if defined(_POSIX_NO_TRUNC) & _POSIX_NO_TRUNC!=-1
  353. #    define LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED 0
  354. #else
  355. #    define LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED 1
  356. #endif
  357.  
  358. #if LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED
  359. #ifdef NAME_MAX
  360. #    define filenametoolong(path) (NAME_MAX < strlen(bindex(path,SLASH)))
  361. #else
  362.     static int
  363. filenametoolong(char *path)
  364. /* Yield true if the last file name in PATH is too long. */
  365. {
  366.     static unsigned long dot_namemax;
  367.  
  368.     register size_t namelen;
  369.     register char *lastslash;
  370.     register unsigned long namemax;
  371.  
  372.     lastslash = strrchr(path, SLASH);
  373.     namelen = strlen(lastslash ? lastslash+1 : path);
  374.     if (namelen <= _POSIX_NAME_MAX) /* fast check for shorties */
  375.         return false;
  376.     if (lastslash) {
  377.         *lastslash = 0;
  378.         namemax = pathconf(path, _PC_NAME_MAX);
  379.         *lastslash = SLASH;
  380.     } else {
  381.         /* Cache the results for the working directory, for speed. */
  382.         if (!dot_namemax)
  383.             dot_namemax = pathconf(".", _PC_NAME_MAX);
  384.         namemax = dot_namemax;
  385.     }
  386.     /* If pathconf() yielded -1, namemax is now ULONG_MAX.  */
  387.     return namemax<namelen;
  388. }
  389. #endif
  390. #endif
  391.  
  392.     void
  393. bufalloc(register struct buf *b, size_t size)
  394. /* Ensure *B is a name buffer of at least SIZE bytes.
  395.  * *B's old contents can be freed; *B's new contents are undefined.
  396.  */
  397. {
  398.     if (b->size < size) {
  399.         if (b->size)
  400.             tfree(b->string);
  401.         else
  402.             b->size = sizeof(malloc_type);
  403.         while (b->size < size)
  404.             b->size <<= 1;
  405.         b->string = tnalloc(char, b->size);
  406.     }
  407. }
  408.  
  409.     void
  410. bufrealloc(register struct buf *b, size_t size)
  411. /* like bufalloc, except *B's old contents, if any, are preserved */
  412. {
  413.     if (b->size < size) {
  414.         if (!b->size)
  415.             bufalloc(b, size);
  416.         else {
  417.             while ((b->size <<= 1)  <  size)
  418.                 ;
  419.             b->string = (char *)testrealloc((malloc_type)b->string, b->size);
  420.         }
  421.     }
  422. }
  423.  
  424.     void
  425. bufautoend(struct buf *b)
  426. /* Free an auto buffer at block exit. */
  427. {
  428.     if (b->size)
  429.         tfree(b->string);
  430. }
  431.  
  432.     char *
  433. bufenlarge(register struct buf *b, const char **alim)
  434. /* Make *B larger.  Set *ALIM to its new limit, and yield the relocated value
  435.  * of its old limit.
  436.  */
  437. {
  438.     size_t s = b->size;
  439.     bufrealloc(b, s + 1);
  440.     *alim = b->string + b->size;
  441.     return b->string + s;
  442. }
  443.  
  444.     void
  445. bufscat(struct buf *b, const char *s)
  446. /* Concatenate S to B's end. */
  447. {
  448.     size_t blen  =  b->string ? strlen(b->string) : 0;
  449.     bufrealloc(b, blen+strlen(s)+1);
  450.     VOID strcpy(b->string+blen, s);
  451. }
  452.  
  453.     void
  454. bufscpy(struct buf *b, const char *s)
  455. /* Copy S into B. */
  456. {
  457.     bufalloc(b, strlen(s)+1);
  458.     VOID strcpy(b->string, s);
  459. }
  460.  
  461.  
  462.     FILE *
  463. rcsreadopen(const char *RCSname)
  464. /* Open RCSNAME for reading and yield its FILE* descriptor.
  465.  * Pass this routine to pairfilenames() for read-only access to the file.  */
  466. {
  467.     FILE *f;
  468.     seteid();
  469.     f = fopen(RCSname, "r");
  470.     setrid();
  471.     return f;
  472. }
  473.  
  474.     int
  475. pairfilenames(int argc,
  476.               char **argv,
  477.               FILE *(*rcsopen)P((const char*)),
  478.               int mustread,
  479.               int tostdout)
  480. /* Function: Pairs the filenames pointed to by argv; argc indicates
  481.  * how many there are.
  482.  * Places a pointer to the RCS filename into RCSfilename,
  483.  * and a pointer to the name of the working file into workfilename.
  484.  * If both the workfilename and the RCS filename are given, and tostdout
  485.  * is true, a warning is printed.
  486.  *
  487.  * If the RCS file exists, places its status into RCSstat.
  488.  *
  489.  * If the RCS file exists, it is RCSOPENed for reading, the file pointer
  490.  * is placed into finptr, and the admin-node is read in; returns 1.
  491.  * If the RCS file does not exist and mustread is set, an error is printed
  492.  * and 0 returned.
  493.  * If the RCS file does not exist and !mustread, the admin node
  494.  * is initialized and -1 returned.
  495.  *
  496.  * 0 is returned on all errors, e.g. files that are not regular files.
  497.  */
  498. {
  499.     static struct buf RCSbuf, tempbuf;
  500.  
  501.     register char *p, *arg, *tempfilename, *RCS1;
  502.     const char *purefname, *pureRCSname;
  503.     FILE *lock1;
  504.  
  505.     if (!(arg = *argv)) return 0; /* already paired filename */
  506.     if (*arg == '-') {
  507.         error("%s option is ignored after file names", arg);
  508.         return 0;
  509.     }
  510.  
  511.     /* Allocate buffer temporary to hold the default paired file name. */
  512.     for (purefname = p = arg; *p; )
  513.         switch (*p++) {
  514.             case SLASH:
  515.             purefname = p;
  516.             break;
  517.             /* Beware characters that cause havoc with ci -k. */
  518.             case KDELIM:
  519.             error("RCS file name `%s' contains %c", arg, KDELIM);
  520.             return 0;
  521.             case ' ': case '\n': case '\t':
  522.             error("RCS file name `%s' contains white space", arg);
  523.             return 0;
  524.         }
  525.     bufalloc(&tempbuf, p - purefname + 3);
  526.     tempfilename = tempbuf.string;
  527.  
  528.         /* first check suffix to see whether it is an RCS file or not */
  529. #if RCSSEP
  530.     if (purefname<(p-=2) && p[0]==RCSSEP && p[1]==RCSSUF)
  531. #else
  532.     if (isRCSfilename(arg, purefname))
  533. #endif
  534.     {
  535.                 /* RCS file name given*/
  536.         RCS1 = arg;
  537.         pureRCSname = purefname;
  538.                 /* derive workfilename*/
  539.           VOID strcpy(tempfilename, purefname);
  540.         tempfilename[p - purefname] = 0;
  541.                 /* try to find workfile name among arguments */
  542.         workfilename = findpair(argc-1,argv+1,tempfilename,false);
  543.         } else {
  544.                 /* working file given; now try to find RCS file */
  545.         workfilename = arg;
  546.                 /* derive RCS file name*/
  547.         VOID sprintf(tempfilename,"%s%c%c", purefname, RCSSEP, RCSSUF);
  548.                 /* Try to find RCS file name among arguments*/
  549.         RCS1 = findpair(argc-1,argv+1,tempfilename,true);
  550.                 pureRCSname=bindex(RCS1, SLASH);
  551.         }
  552.         /* now we have a (tentative) RCS filename in RCS1 and workfilename  */
  553.         /* Second, try to find the right RCS file */
  554.         if (pureRCSname!=RCS1) {
  555.                 /* a path for RCSfile is given; single RCS file to look for */
  556.         errno = 0;
  557.         RCSfilename = p = RCS1;
  558.         finptr = (*rcsopen)(RCSfilename = p = RCS1);
  559.         } else {
  560.         /* no path for RCS file name. Prefix it with path of work */
  561.         /* file if RCS file omitted.  Try RCSDIR subdirectory 1st.*/
  562.         bufalloc(&RCSbuf, strlen(workfilename)+sizeof(rcsdir)+2);
  563.         RCSfilename = p = RCSbuf.string;
  564.         if (RCS1==tempfilename) {
  565.             /* RCS file name not given; prepend work path */
  566.             VOID strncpy(p, arg, purefname-arg);
  567.             p += purefname-arg;
  568.         }
  569.         VOID strcpy(p, rcsdir);
  570.         VOID strcpy(p+sizeof(rcsdir)-1, RCS1);
  571.  
  572.         /* Try D/RCS/file,v. */
  573.         errno = 0;
  574.         if (!(finptr = (*rcsopen)(RCSfilename))
  575.             &&  (errno==ENOTDIR || errno==ENOENT)
  576.             /*
  577.              * Many (broken) systems yield ENOENT, not ENOTDIR,
  578.              * when the problem is a missing RCS subdirectory.
  579.              */
  580.         ) {
  581.             lock1 = frewrite;
  582.  
  583.             /* Try D/file,v. */
  584.             VOID strcpy(p, RCS1);
  585.             errno = 0;
  586.             if (!(finptr=(*rcsopen)(RCSfilename)) && errno==ENOENT) {
  587.                 /*
  588.                  * Neither file exists; determine the default.
  589.                  * Prefer D/RCS/file,v to D/file,v.
  590.                  */
  591.                 if (mustread || lock1) {
  592.                 /* Switch back to D/RCS/file,v. */
  593.                 VOID strcpy(p, rcsdir);
  594.                 VOID strcpy(p+sizeof(rcsdir)-1, RCS1);
  595.                 }
  596.             }
  597.         }
  598.         p = RCSbuf.string;
  599.         }
  600.     if (finptr) {
  601.         if (fstat(fileno(finptr), &RCSstat) < 0)
  602.             efaterror(p);
  603.         if (!S_ISREG(RCSstat.st_mode)) {
  604.             error("%s isn't a regular file -- ignored", p);
  605.                         return 0;
  606.                 }
  607.                 Lexinit(); getadmin();
  608.     } else {
  609.         if (errno!=ENOENT || mustread || !frewrite) {
  610.             if (errno == EEXIST)
  611.                 error("RCS file %s is in use", p);
  612.             else
  613.                 eerror(p);
  614.             return 0;
  615.         }
  616.                 InitAdmin();
  617.         };
  618. #    if LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED
  619.         if (filenametoolong(p)) {
  620.         error("RCS file name %s is too long", p);
  621.         return 0;
  622.         }
  623. #        ifndef NAME_MAX
  624.         /*
  625.          * Check workfilename, even though it is shorter,
  626.          * because it may reside on a different filesystem.
  627.          */
  628.         if (filenametoolong(workfilename)) {
  629.             error("working file name %s is too long", workfilename);
  630.             return 0;
  631.         }
  632. #        endif
  633. #    endif
  634.  
  635.         if (tostdout&&
  636.             !(RCS1==tempfilename||workfilename==tempfilename))
  637.                 /*The last term determines whether a pair of        */
  638.                 /* file names was given in the argument list        */
  639.                 warn("Option -p is set; ignoring output file %s",workfilename);
  640.  
  641.     return finptr ? 1 : -1;
  642. }
  643.  
  644.  
  645.     const char *
  646. getfullRCSname()
  647. /* Function: returns a pointer to the full path name of the RCS file.
  648.  * Gets the working directory's name at most once.
  649.  * Removes leading "../" and "./".
  650.  */
  651. {
  652.     static const char *wd;
  653.     static struct buf rcsbuf, wdbuf;
  654.     static size_t pathlength;
  655.  
  656.     register const char *realname;
  657.     register size_t parentdirlength;
  658.     register unsigned dotdotcounter;
  659.     register char *d;
  660.  
  661.     if (ROOTPATH(RCSfilename)) {
  662.                 return(RCSfilename);
  663.         } else {
  664.         if (!wd) { /* Get working directory for the first time. */
  665.             if (!(d = cgetenv("PWD"))) {
  666.             bufalloc(&wdbuf, 1 +
  667. #                ifdef PATH_MAX
  668.                 PATH_MAX
  669. #                else
  670.                 _POSIX_PATH_MAX
  671. #                endif
  672.             );
  673.             errno = 0;
  674. #            if !has_getcwd
  675.                 d = getwd(wdbuf.string);
  676. #            else
  677.                 while (
  678.                     !(d = getcwd(wdbuf.string,(int)wdbuf.size))
  679.                 &&  errno==ERANGE
  680.                 )
  681.                 bufalloc(&wdbuf, wdbuf.size<<1);
  682. #            endif
  683.             if (!d)
  684.                 efaterror("working directory");
  685.             }
  686.             pathlength = strlen(d);
  687.             while (pathlength && d[pathlength-1]==SLASH) {
  688.             d[--pathlength] = 0;
  689.                         /* Check needed because some getwd implementations */
  690.                         /* generate "/" for the root.                      */
  691.                     }
  692.             wd = d;
  693.                 }
  694.                 /*the following must be redone since RCSfilename may change*/
  695.         /* Find how many `../'s to remove from RCSfilename.  */
  696.                 dotdotcounter =0;
  697.                 realname = RCSfilename;
  698.                 while( realname[0]=='.' &&
  699.                       (realname[1]==SLASH||(realname[1]=='.'&&realname[2]==SLASH))){
  700.                         if (realname[1]==SLASH) {
  701.                             /* drop leading ./ */
  702.                             realname += 2;
  703.                         } else {
  704.                             /* drop leading ../ and remember */
  705.                             dotdotcounter++;
  706.                             realname += 3;
  707.                         }
  708.                 }
  709.         /* Now remove dotdotcounter trailing directories from wd. */
  710.         parentdirlength = pathlength;
  711.         while (dotdotcounter && parentdirlength) {
  712.                     /* move pointer backwards over trailing directory */
  713.             if (wd[--parentdirlength] == SLASH) {
  714.                         dotdotcounter--;
  715.                     }
  716.                 }
  717.         if (dotdotcounter) {
  718.                     error("can't generate full path name for RCS file");
  719.                     return RCSfilename;
  720.                 } else {
  721.                     /* build full path name */
  722.             bufalloc(&rcsbuf, parentdirlength+strlen(realname)+2);
  723.             VOID strncpy(rcsbuf.string, wd, parentdirlength);
  724.             rcsbuf.string[parentdirlength++] = SLASH;
  725.             VOID strcpy(rcsbuf.string+parentdirlength, realname);
  726.             return rcsbuf.string;
  727.         }
  728.         }
  729. }
  730.  
  731.     const char *
  732. tmp()
  733. /* Yield the name of the tmp directory, with a trailing SLASH.  */
  734. {
  735.     static const char *s;
  736.     if (!s)
  737.         if (!(s = getenv("TMP")))
  738.             s = TMPDIR;
  739.         else {
  740.             size_t l = strlen(s);
  741.             int extra = l && s[l-1]!=SLASH;
  742.             char *p = ftnalloc(char, l + extra + 1);
  743.             VOID strcpy(p, s);
  744.             if (extra) {
  745.                 p[l] = SLASH;
  746.                 p[l+1] = 0;
  747.             }
  748.             s = p;
  749.         }
  750.     return s;
  751. }
  752.  
  753.  
  754. #if !has_rename | bad_rename
  755.  
  756.     int
  757. re_name(const char *from, const char *to)
  758. /* Function: renames a file with the name given by from to the name given by to.
  759.  * unlinks the to-file if it already exists. returns -1 on error, 0 otherwise.
  760.  */
  761. {       VOID unlink(to);      /* no need to check return code; will be caught by link*/
  762.                          /* no harm done if file "to" does not exist            */
  763. #if has_rename
  764.     return rename(from,to);
  765. #else
  766.         if (link(from,to)<0) return -1;
  767.         return(unlink(from));
  768. #endif
  769. }
  770.  
  771. #endif
  772.  
  773.  
  774. #if !has_getcwd & !has_getwd
  775.  
  776. #if !MAKEDEPEND
  777. #include <sys/dir.h>
  778. #endif
  779.  
  780.  
  781. #define dot     "."
  782. #define dotdot  ".."
  783.  
  784.  
  785.  
  786. char * getwd(char *name)
  787. /* Function: places full pathname of current working directory into name and
  788.  * returns name on success, NULL on failure.
  789.  * getwd is an adaptation of pwd. May not return to the current directory on
  790.  * failure.
  791.  */
  792. {
  793.         FILE    *file;
  794.         struct  stat    d, dd;
  795.         char buf[2];    /* to NUL-terminate dir.d_name */
  796.         struct  direct  dir;
  797.  
  798.         int rdev, rino;
  799.         int off;
  800.         register i,j;
  801.  
  802.     off = 0;
  803.     name[0] = SLASH;
  804.         name[1] = '\0';
  805.         buf[0] = '\0';
  806.     if (stat(name, &d)<0) return NULL;
  807.         rdev = d.st_dev;
  808.         rino = d.st_ino;
  809.         for (;;) {
  810.                 if (stat(dot, &d)<0) return NULL;
  811.                 if (d.st_ino==rino && d.st_dev==rdev) {
  812.             if (name[off] == SLASH)
  813.                 name[off] = '\0';
  814.             VOID chdir(name); /*change back to current directory*/
  815.                         return name;
  816.                 }
  817.                 if ((file = fopen(dotdot,"r")) == NULL) return NULL;
  818.                 if (fstat(fileno(file), &dd)<0) goto fail;
  819.         VOID chdir(dotdot);
  820.                 if(d.st_dev == dd.st_dev) {
  821.                         if(d.st_ino == dd.st_ino) {
  822.                 if (name[off] == SLASH)
  823.                 name[off] = 0;
  824.                 VOID chdir(name); /*change back to current directory*/
  825.                 ffclose(file);
  826.                             return name;
  827.                         }
  828.                         do {
  829.                             if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
  830.                                 goto fail;
  831.                         } while (dir.d_ino != d.st_ino);
  832.                 }
  833.                 else do {
  834.                         if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
  835.                             goto fail;
  836.                         }
  837.                         if (dir.d_ino == 0)
  838.                 dd.st_ino = d.st_ino + 1;
  839.                         else if (stat(dir.d_name, &dd) < 0)
  840.                 goto fail;
  841.                 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  842.         ffclose(file);
  843.  
  844.                 /* concatenate file name */
  845.                 i = -1;
  846.                 while (dir.d_name[++i] != 0);
  847.                 for(j=off+1; j>0; --j)
  848.                         name[j+i+1] = name[j];
  849.                 off=i+off+1;
  850.         name[i+1] = SLASH;
  851.                 for(--i; i>=0; --i)
  852.                         name[i+1] = dir.d_name[i];
  853.         } /* end for */
  854.  
  855. fail:   ffclose(file);
  856.         return NULL;
  857. }
  858.  
  859.  
  860. #endif
  861.  
  862.  
  863. #ifdef PAIRTEST
  864. /* test program for pairfilenames() and getfullRCSname() */
  865.  
  866. const char cmdid[] = "pair";
  867.  
  868. main(int argc, char *argv[])
  869. {
  870.         int result;
  871.         int initflag,tostdout;
  872.         quietflag=tostdout=initflag=false;
  873.  
  874.         while(--argc, ++argv, argc>=1 && ((*argv)[0] == '-')) {
  875.                 switch ((*argv)[1]) {
  876.  
  877.                 case 'p':       tostdout=true;
  878.                                 break;
  879.                 case 'i':       initflag=true;
  880.                                 break;
  881.                 case 'q':       quietflag=true;
  882.                                 break;
  883.                 default:        error("unknown option: %s", *argv);
  884.                                 break;
  885.                 }
  886.         }
  887.  
  888.         do {
  889.                 RCSfilename=workfilename=nil;
  890.         result = pairfilenames(argc,argv,rcsreadopen,!initflag,tostdout);
  891.                 if (result!=0) {
  892.             diagnose("RCS file: %s; working file: %s\nFull RCS file name: %s\n",
  893.                  RCSfilename,workfilename,getfullRCSname()
  894.             );
  895.                 }
  896.                 switch (result) {
  897.                         case 0: continue; /* already paired file */
  898.  
  899.                         case 1: if (initflag) {
  900.                                     error("RCS file %s exists already",RCSfilename);
  901.                                 } else {
  902.                     diagnose("RCS file %s exists\n",RCSfilename);
  903.                                 }
  904.                 ffclose(finptr);
  905.                                 break;
  906.  
  907.             case -1:diagnose("RCS file doesn't exist\n");
  908.                                 break;
  909.                 }
  910.  
  911.         } while (++argv, --argc>=1);
  912.  
  913. }
  914.  
  915. void exiterr P((void))
  916. {
  917.     printf("Exiterr - exit !\n");
  918.     exit(1);
  919. }
  920. #endif
  921.